package com.contest.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.contest.dao.RoomInfoDao;
import com.contest.entity.BettingRecord;
import com.contest.entity.RoomInfo;
import com.contest.entity.UserInfo;
import com.contest.service.BettingRecordService;
import com.contest.service.RoomInfoService;
import com.contest.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoDao, RoomInfo> implements RoomInfoService {

    @Autowired
    BettingRecordService bettingRecordService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    RoomInfoDao roomInfoDao;
    @Autowired
    SettlementService settlementService;

    @Override
    @Transactional
    public boolean updateResult(RoomInfo roomInfo) {
        Integer result = roomInfo.getFdResult();
//        float todayAmount =
        if (result != 0) {
            float aTotalScale = 0.0f;
            float bTotalScale = 0.0f;
            float aSingleScale = 0.0f;
            float bSingleScale = 0.0f;
            String winId;
            float sizeScale = roomInfo.getFdTotalSizeScale().floatValue();
            float specialScale = roomInfo.getFdSpecialScale().floatValue();
            int totalKillNum = roomInfo.getFdTotalKill();
            String letId = roomInfo.getFdLetClanId();
            float letNum = roomInfo.getFdLetNum().floatValue();
            float aWinNum = roomInfo.getFdClanAWinNum().floatValue();
            float bWinNum = roomInfo.getFdClanBWinNum().floatValue();
            float totalWinNum = aWinNum + bWinNum;
            float winNum = totalWinNum / 2;
            String totalWinId;
            if (letId.equals(roomInfo.getFdClanAId())) {
                bWinNum += letNum;
            } else if (letId.equals(roomInfo.getFdClanBId())) {
                bWinNum += letNum;
            }
            if (aWinNum > winNum) {
                aTotalScale = roomInfo.getFdClanATotalScale().floatValue();
                totalWinId = roomInfo.getFdClanAId();
                computeTotal(totalWinId, aTotalScale, roomInfo.getFdId());
            } else if (bWinNum > winNum) {
                bTotalScale = roomInfo.getFdClanBTotalScale().floatValue();
                totalWinId = roomInfo.getFdClanBId();
                computeTotal(totalWinId, bTotalScale, roomInfo.getFdId());
            }
            if (result == -1) {
                //A胜
//                aSingleScale = roomInfo.getFdClanAScale().floatValue();
//                winId = roomInfo.getFdClanAId();
//                computeSingle(roomInfo.getFdId(), winId, aSingleScale);
                roomInfo.setFdResult(-1);
            } else if (result == 1) {
                //B胜
//                winId = roomInfo.getFdClanBId();
//                bSingleScale = roomInfo.getFdClanBScale().floatValue();
//                computeSingle(roomInfo.getFdId(), winId, bSingleScale);
                roomInfo.setFdResult(1);
            }
            settlementService.computeSize(totalKillNum+"", roomInfo.getFdId(), sizeScale);
            settlementService.computeSpecial(totalKillNum+"", specialScale, roomInfo.getFdId());
            roomInfo.setFdIsSettle(1);
            this.updateById(roomInfo);
            return true;
        } else {
            return this.updateById(roomInfo);
        }
    }

    @Override
    public List<RoomInfo> getList() {
        return roomInfoDao.getList();
    }

    /**
     * 计算总场
     * @param totalWinId
     * @param totalScale
     * @param roomId
     */
    private void computeTotal(String totalWinId, float totalScale, String roomId) {
        EntityWrapper<BettingRecord> recordEntityWrapper = new EntityWrapper<BettingRecord>();
        recordEntityWrapper.addFilter("fd_type={0} and fd_room_id={1} and fd_is_settlement={2}", 2, roomId, 0);
        List<BettingRecord> recordList = bettingRecordService.selectList(recordEntityWrapper);
        settlementService.computeWin(recordList, totalScale, totalWinId);
    }




}
